/**
 * @file pp_buffer.h
 * @author monster
 * @date 2024/4/15
 */

#ifndef PP_BUFFER_H
#define PP_BUFFER_H

#ifdef __cplusplus
extern "C" {
#endif

/*********************
 *      INCLUDES
 *********************/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>

/*********************
 *      DEFINES
 *********************/


/**********************
 *      MACROS
 **********************/


/**********************
 *      TYPEDEFS
 **********************/

typedef struct _pp_buffer_t {
    void             *buffer[2];
    uint32_t         size;
    volatile uint8_t write_index;
    volatile uint8_t read_index;
    volatile uint8_t read_available[2];
} pp_buffer_t;


/**********************
 * GLOBAL PROTOTYPES
 **********************/

/**
 * @brief Init ping pong buffer.
 * @param ppbuf pointer struct pp_buffer_t
 * @param buf0 pointer chache 0
 * @param buf1 pointer chache 1
 * @param size single buffer size
 */
void pp_buffer_init(pp_buffer_t *ppbuf, void *buf0, void *buf1, uint32_t size);

bool pp_buffer_get_read_buffer(pp_buffer_t *ppbuf, void **p_buf);
void pp_buffer_set_read_done(pp_buffer_t *ppbuf);
bool pp_buffer_read_data(pp_buffer_t *ppbuf, void *p_buf, uint32_t size);
void pp_buffer_get_write_buffer(pp_buffer_t *ppbuf, void **p_buf);
void pp_buffer_set_write_done(pp_buffer_t *ppbuf);
void pp_buffer_write_data(pp_buffer_t *ppbuf, void *p_buf, uint32_t size);

#ifdef __cplusplus
}
#endif

#endif
